﻿Imports System.Data
Imports System.Data.SqlClient
Imports PBE.Market.Logic.Contracts

Namespace Data



    Public Class cDataWriterDR
        Private _con As New SqlConnection(My.Settings.PBEConnectionString)
        Private _cmdContract As String
        Private _cmdPayment As String
        Private _cmdID As String

        Sub New()
            MyBase.New()
            _cmdContract = "INSERT INTO [MaturityLadder].[dbo].[tblContracts] " &
                                "([ContractNumber]" &
                                ",[ContractType]" &
                                ",[ContractCurrency]" &
                                ",[ContractMajorGroup]" &
                                ",[ContractProductCode]" &
                                ",[ContractProductDiscription]" &
                                ",[ContractTag]" &
                                ",[ContractResponsableGL]" &
                                ",[ContractCycleOfPayment]" &
                                ",[ContractMaturityDate]" &
                                ",[ContractBalance]" &
                                ",[ContractInterestRate])" &
                            "VALUES" &
                                "({0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11})" ' ; select IDENT_CURRENT('tblContracts')"

            _cmdPayment = "INSERT INTO [MaturityLadder].[dbo].[tblPayments]" &
                            "([ContractID]" &
                            ",[PaymentOrder]" &
                            ",[PaymentDate]" &
                            ",[PaymentDays]" &
                            ",[PaymnetAmount]" &
                            ",[PaymentPrincipalAmount]" &
                            ",[PaymentInterestAmount]" &
                            ",[tenor])" &
                         " VALUES" &
                            "({0},{1},{2},{3},{4},{5},{6},{7})"

            _cmdID = "select IDENT_CURRENT('tblContracts') "

            _con.Open()
        End Sub

        Sub CloseConnection()

            _con.Close()
        End Sub

        Sub Disposte()
            Me.Disposte()

        End Sub


        Sub SaveData(ByVal cnt As iContract)
            Dim strCmd As String
            strCmd = String.Format(_cmdContract, "'" & cnt.ContractID & "'",
                                  "'" & cnt.ContractType & "'",
                                  "'" & cnt.Currency & "'",
                                  "'" & cnt.ContractMajorGroup & "'",
                                  "'" & cnt.ContractProductCode & "'",
                                  "'" & cnt.ContractProductDiscription & "'",
                                  "'" & cnt.ContractTag & "'",
                                  "'" & cnt.ResponsableGL & "'",
                                  "'" & cnt.CycleOfPayment & "'",
                                  "'" & cnt.MaturityDate & "'",
                                  cnt.Balance,
                                  cnt.InterestRate)
            Dim _sCmdContact As New SqlCommand(strCmd)
            _sCmdContact.Connection = _con
            _sCmdContact.ExecuteNonQuery()

            Dim _sCmdID As New SqlCommand(_cmdID, _con)
            Dim intID As Integer = _sCmdID.ExecuteScalar

            For Each p As PaymentSchedual In cnt.PaymentSchedaul
                strCmd = String.Format(_cmdPayment,
                                               intID,
                                               p.intPaymentOrder,
                                               "'" & p.dtePaymentDate & "'",
                                               p.intPaymentDays,
                                               p.dblPaymentAmount,
                                               p.dblPrincipalPayment,
                                               p.dblInterestPayment,
                                               p.strTenor)
                Dim _sCmdPayment As New SqlCommand(strCmd, _con)
                _sCmdPayment.ExecuteNonQuery()
            Next


        End Sub
    End Class

End Namespace